<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" :index="indexMethod" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">
                    分配权限
                  </el-button>
                  <el-button size="mini" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-size="pageParams.pagesize"
                :current-page="pageParams.page"
                :page-sizes="[1, 2, 3]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @current-change="hCurrentChange"
                @size-change="handleSizeChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="close"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showDialogAssign"
    >
      <assign-permission v-if="showDialogAssign" :role-id="roleId" @close="showDialogAssign=false" />
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, delRole, addRoles, editRoles } from '@/api/setting'
import assignPermission from './assignPermission'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      roles: [],
      roleId: '',
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2 // 每页两条  --- 要与pagination中page-size一致
      },
      total: null, // 总的数据条数,
      showDialog: false,
      showDialogAssign: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      isEdit: true
    }
  },
  computed: {
    maxNum() {
      return Math.ceil(this.total / this.pageParams.pagesize)
    },
    isLastPageFulled() {
      return this.total % this.pageParams.pagesize === 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    indexMethod(index) {
      return (this.pageParams.page - 1) * this.pageParams.pagesize + index + 1
    },
    hCurrentChange(curpage) {
      this.pageParams.page = curpage
      this.loadRoles()
    },
    handleSizeChange(curpagesize) {
      this.pageParams.pagesize = curpagesize
      this.loadRoles()
    },
    async doDelRole(id) {
      await delRole(id)
      if (this.roles.length === 1) {
        this.pageParams.page--
        if (this.pageParams.page < 1) {
          this.pageParams.page = 1
        }
      }
      this.loadRoles()
    },
    hDel(id) {
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.doDelRole(id)
      }).catch((err) => {
        // 用户取消退出
        console.log(err)
      })
    },
    async doAdd() {
      // 发请求添加角色
      await addRoles(this.roleForm)
      // 跳到最后一页
      if (this.isLastPageFulled) {
        this.pageParams.page = this.maxNum + 1
        this.total++
      } else {
        this.pageParams.page = this.maxNum
      }
      // 重新渲染页面
      this.loadRoles()
      // 关闭弹窗
      this.showDialog = false
    },
    async doEdit() {
      await editRoles(this.roleForm)
      // 重新渲染页面
      this.loadRoles()
      // 关闭弹窗
      this.showDialog = false
    },
    // 添加函数
    hSubmit() {
      const { doAdd, isEdit, doEdit } = this
      isEdit ? doEdit() : doAdd()
    },
    // 用户点击了编辑
    hEdit(row) {
      // 1. 把当前的数据直接给表单
      this.roleForm = { ...row }
      this.isEdit = true
      // 2. 显示弹层
      this.showDialog = true
      // console.log(row)
    },
    hAdd() {
      this.isEdit = false
      // 2. 显示弹层
      this.showDialog = true
    },
    close() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    },
    hAssign(id) {
      this.showDialogAssign = true
      this.roleId = id
    }
  }
}
</script>
